package erfgame.frontend.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import erfgame.frontend.desktop.controller.ControllerCompletionListener;
import erfgame.frontend.desktop.controller.MainController;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	
	private MainController controller;
	
	public MainFrame() {
		super( "Amber" );
		this.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
	}
	
	public void init( MainController mainController ) {
		Container contentPanel = this.getContentPane();
		contentPanel.setLayout( new BorderLayout() );
		contentPanel.setPreferredSize( new Dimension( 480, 320 ) );
		mainController.addControllerCompletionListener( new ControllerCompletionListener() {
			public void controllerCompleted(Object source) {
				System.exit( 0 );
			}
		});
		mainController.start( contentPanel, BorderLayout.CENTER );		
		this.controller = mainController;
		this.addWindowListener( new WindowListener() {
			
			public void windowOpened(WindowEvent e) {
				
			}
			
			public void windowIconified(WindowEvent e) {
				
			}
			
			public void windowDeiconified(WindowEvent e) {
				
			}
			
			public void windowDeactivated(WindowEvent e) {
				
			}
			
			public void windowClosing(WindowEvent e) {
				controller.close();
				MainFrame.this.dispose();
			}
			
			public void windowClosed(WindowEvent e) {
				System.exit( 0 );
			}
			
			public void windowActivated(WindowEvent e) {
				
			}
		});
	}
	
	public static final void main( String[] args ) {
		// TODO : get the main frame from the context
		
		MainFrame mainFrame = new MainFrame();
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/beans/ui.xml");
		MainController mainController;
		mainController = (MainController)context.getBean("mainController");
	
		mainFrame.init(
				mainController
		);
		mainFrame.pack();
		mainFrame.setVisible( true );
	}
}
